package com.hsy.bibc.modules.gathering;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.widget.DPCircleImageView;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.modules.main.MainActivity;
import com.hsy.bibc.modules.common.CommitOkActivity;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;
import com.hsy.bibc.utils.VolleyLoadPicture;

import java.util.HashMap;
import java.util.Map;

public class GatheringActivity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {

    protected DPCircleImageView avtar;
    protected TextView nickName;
    protected TextView description;
    protected TextView confirmButtom;
    protected EditText input;

    private ScanResultData.Result scanResult;

    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                scanResult = DPGsonUtil.fromJson(response, ScanResultData.Result.class);
                nickName.setText(scanResult.getNickname());
                description.setText(String.format("可用余额: ¥ %s", scanResult.getScore()));
                input.setHint(String.format("本次最多可支付¥ %s", scanResult.getScore()));
//                DPPicassoUtil.load(GatheringActivity.this, scanResult.getAvatar(), avtar);
                VolleyLoadPicture vlp = new VolleyLoadPicture(GatheringActivity.this, avtar);
                vlp.getmImageLoader().get(scanResult.getAvatar(), vlp.getOne_listener());
                CommonUtils.hideProgressDialog();
            } else if (1 == what) {
                hideConfirmDialog();
                CommitResultData.Result result = DPGsonUtil.fromJson(response, CommitResultData.Result.class);
                startActivity(CommitOkActivity.class, "3", result.getOrder_id(), result.getOrder_sn());
                CommonUtils.hideProgressDialog();
                finish();
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {

            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            if (0 == what) {
                if (Consts.TYPE_CODE_ERROR == type) {
                    CommonUtils.showErrorMsgDialog(GatheringActivity.this, msg);
                }
            } else if (1 == what) {
                CommonUtils.hideProgressDialog();
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.txt_gathering);
        setContentView(R.layout.activity_gathering);

        initView();

        CommonUtils.showProgressDialog(this);
        Map<String, String> params = new HashMap<>();
        params.put("msg", getActivityStartExtra()[0]);
        HttpUtil.httpPostWithParams(this, 0, Consts.Apis.GET_USER_INFO, params, callback);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.confirm_buttom) {

            if (confirmButtom.isEnabled()) {
                showConfirmDialog(GatheringActivity.this, "消费金额为" + input.getText().toString(), "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hideConfirmDialog();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommonUtils.showProgressDialog(GatheringActivity.this);
                        Map<String, String> params = new HashMap<>();
                        params.put("uid", scanResult.getUid());
                        params.put("sign", scanResult.getSign());
                        params.put("money", input.getText().toString());
                        HttpUtil.httpPostWithParams(GatheringActivity.this, 1, Consts.Apis.POST_COMMIT_SY, params, callback);
                    }
                }, null);

            }
        }
    }

    private boolean checkThrough() {
        if (isEmpty(input.getText().toString())) {
            showToast("请输入支付数额");
            return false;
        }
        if (".".equals(input.getText().toString().trim().substring(0))) {
            input.setText("0.");
            input.setSelection(2);
        }
        if (input.getText().toString().equals("0") || input.getText().toString().substring(input.getText().length() - 1).equals(".")) {
            return false;
        }

        float i = Float.parseFloat(input.getText().toString().trim());
        if (i > Float.parseFloat(scanResult.getScore())) {
            showToast(String.format("本次最多可支付¥ %s", scanResult.getScore()));
            return false;
        }
        return true;
    }

    private void initView() {
        avtar = (DPCircleImageView) findViewById(R.id.avtar);
        nickName = (TextView) findViewById(R.id.nick_name);
        description = (TextView) findViewById(R.id.description);
        confirmButtom = (TextView) findViewById(R.id.confirm_buttom);
        confirmButtom.setOnClickListener(GatheringActivity.this);
        input = (EditText) findViewById(R.id.input);

        confirmButtom.setEnabled(false);

        input.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                CommonUtils.setPricePoint(input);
            }

            @Override
            public void afterTextChanged(Editable s) {

                confirmButtom.setEnabled(checkThrough());
            }
        });
    }

    public static class CommitResultData {
        private String error;

        private String msg;

        private Result result;

        public void setError(String error) {
            this.error = error;
        }

        public String getError() {
            return this.error;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getMsg() {
            return this.msg;
        }

        public void setResult(Result result) {
            this.result = result;
        }

        public Result getResult() {
            return this.result;
        }

        public static class Result {
            private String order_id;

            private String order_sn;

            public void setOrder_id(String order_id) {
                this.order_id = order_id;
            }

            public String getOrder_id() {
                return this.order_id;
            }

            public void setOrder_sn(String order_sn) {
                this.order_sn = order_sn;
            }

            public String getOrder_sn() {
                return this.order_sn;
            }

        }
    }

    public static class ScanResultData {
        private String error;

        private String msg;

        private Result result;

        public void setError(String error) {
            this.error = error;
        }

        public String getError() {
            return this.error;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getMsg() {
            return this.msg;
        }

        public void setResult(Result result) {
            this.result = result;
        }

        public Result getResult() {
            return this.result;
        }

        public static class Result {
            private String id;

            private String score;

            private String avatar;

            private String nickname;

            private String uid;

            private String sign;

            public void setId(String id) {
                this.id = id;
            }

            public String getId() {
                return this.id;
            }

            public void setScore(String score) {
                this.score = score;
            }

            public String getScore() {
                return this.score;
            }

            public void setAvatar(String avatar) {
                this.avatar = avatar;
            }

            public String getAvatar() {
                return this.avatar;
            }

            public void setNickname(String nickname) {
                this.nickname = nickname;
            }

            public String getNickname() {
                return this.nickname;
            }

            public void setUid(String uid) {
                this.uid = uid;
            }

            public String getUid() {
                return this.uid;
            }

            public void setSign(String sign) {
                this.sign = sign;
            }

            public String getSign() {
                return this.sign;
            }

        }
    }
}
